import matplotlib.pyplot as plt
import numpy as np

# 图像数据 二维数组形式显示像素值
def line(x1,x2, y1,y2):
    length = abs(y2-y1) if abs(y2-y1)>=abs(x2-x1) else abs(x2-x1)
    delta_x = (x2-x1)/length; delta_y = (y2-y1)/length
    x = x1; y = y1
    # 坐标
    img = np.zeros((200,200))
    for i in range(1,length+1):
        img[200-int(y)-1, int(x)] = 255
        x += delta_x; y += delta_y
    return img

img = line(10,190, 126,60)

# 保存图像
plt.imsave("直线.png", img, cmap='gray_r')

# 显示图像
fig=plt.figure()
ax = fig.add_subplot(111)
ax.imshow(img, cmap='gray_r')
plt.show()